Sentiment with Bing

library(tidyverse)
library(tidytext)
library(plotly)
library(wordcloud)

seinfeld <- read.csv("scripts.csv", stringsAsFactors = FALSE)

sentiments <- get_sentiments("bing")

text_words <- seinfeld %>% 
    as_tibble() %>% 
    tidytext::unnest_tokens(output = word, input = Dialogue, token = "words") %>% 
    inner_join(sentiments, by = "word") 

seinfeld_plot <- text_words %>% 
    group_by(Season) %>% 
    count(sentiment, EpisodeNo) %>% 
    # filter(Character %in% c("ELAINE", "GEORGE", "JERRY", "KRAMER")) %>% 
    spread(sentiment, n) %>% 
    na.omit() %>% 
    mutate(raw_score = positive - negative,
           positivity = positive/(positive + negative),
           offset_positivity = positivity - .5,
           offset = mean(positive - negative),
           offset_score = raw_score - offset) %>% 
    # mutate(Character = reorder(Character, offset_score)) %>%
    ggplot(aes(x = EpisodeNo, y = offset_positivity)) +
    geom_col() +
    facet_grid(.~ Season)

ggplotly(seinfeld_plot)

Sentiments with AFINN

sentiments <- get_sentiments("afinn")

text_words <- seinfeld %>% 
    as_tibble() %>% 
    tidytext::unnest_tokens(output = word, input = Dialogue, token = "words") %>% 
    inner_join(sentiments, by = "word") 

seinfeld_plot <- text_words %>% 
    group_by(Season, EpisodeNo) %>% 
    summarize(sum_score = sum(score)) %>% 
    ggplot(aes(x = EpisodeNo, y = sum_score)) +
    geom_col() +
    facet_grid(.~ Season)

ggplotly(seinfeld_plot)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

Word clouds

text_words <- seinfeld %>% 
    as_tibble() %>% 
    tidytext::unnest_tokens(output = word, input = Dialogue, token = "words") %>% 
    filter(!word %in% c("yeah", "no", "like", "good", "want", "yes")) %>%
    filter(!word %in% c("hey", "jerry", "george", "elaine", "kramer", "uh", "gonna")) %>%
    anti_join(stop_words)
## Joining, by = "word"

Jerry

text_words %>% 
    filter(Character == "JERRY") %>% 
    # anti_join(stop_words) %>% 
    count(word) %>%
    with(wordcloud(word, n, max.words = 100))
## Warning in wordcloud(word, n, max.words = 100): supposed could not be fit
## on page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): ya could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): love could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): newman could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): taking could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): date could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): matter could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): shirt could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): door could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): dont could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): boy could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): nice could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): day could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): woman could not be fit on
## page. It will not be plotted.

George

text_words %>% 
    filter(Character == "GEORGE") %>% 
    # anti_join(stop_words) %>% 
    count(word) %>%
    with(wordcloud(word, n, max.words = 100))
## Warning in wordcloud(word, n, max.words = 100): hands could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): laughs could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): supposed could not be fit
## on page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): guy could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): ha could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): happen could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): thinking could not be fit
## on page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): office could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): god could not be fit on
## page. It will not be plotted.

Elaine

text_words %>% 
    filter(Character == "ELAINE") %>% 
    # anti_join(stop_words) %>% 
    count(word) %>%
    with(wordcloud(word, n, max.words = 100))
## Warning in wordcloud(word, n, max.words = 100): guy could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): listen could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): phone could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): talking could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): alright could not be fit on
## page. It will not be plotted.
## Warning in wordcloud(word, n, max.words = 100): coming could not be fit on
## page. It will not be plotted.

Kramer

text_words %>% 
    filter(Character == "KRAMER") %>% 
    # anti_join(stop_words) %>% 
    count(word) %>%
    with(wordcloud(word, n, max.words = 100))
## Warning in wordcloud(word, n, max.words = 100): wait could not be fit on
## page. It will not be plotted.

Newman

text_words %>% 
    filter(Character == "NEWMAN") %>% 
    # anti_join(stop_words) %>% 
    count(word) %>%
    with(wordcloud(word, n, max.words = 100))